/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pt.isel.beans;

import java.io.Serializable;
import java.text.ParseException;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;

/**
 * just a bean from this enum: public static enum Action { Delete, Add }
 *
 * @author fallen
 */
@ManagedBean(name = "validate")
@ViewScoped
public class ValidateBean implements Serializable {

    private boolean render = false;
    private int column = 2;

    //<editor-fold defaultstate="collaped" desc="IDE Generated">
    public int getColumn() {
        return column;
    }

    public void setColumn(int column) {
        this.column = column;
    }

    public boolean isRender() {
        return render;
    }

    public void setRender(boolean render) {
        this.render = render;
    }
    //</editor-fold>

    public ValidateBean() {
        render = false;
        column = 2;
    }

    public void validateDate(FacesContext context,
            UIComponent toValidate,
            Object value) {
        String email = (String) value;
        try {
            SelectBean.dateFormat.parse(email);
            render = false;
            column = 2;
        } catch (ParseException ex) {
            ((UIInput) toValidate).setValid(false);
            FacesMessage message = new FacesMessage(String.format(
                    "Invalid date format. Must be %s", SelectBean.dateFormatAsString));
            context.addMessage(toValidate.getClientId(context), message);
            render = true;
            column = 3;
        }
    }
}
